package com.lemon.smartsafetynew.ui.activity.record;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingCheckItemVo;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingItemVo;
import com.lemon.smartsafetynew.ui.activity.ledger.LedgerBuildingCheckItemDetailActivity;
import com.lemon.smartsafetynew.ui.fragment.hidden.SafeHiddenListForGovFragment;
import com.lemon.smartsafetynew.ui.fragment.ledger.LedgerBuildingCheckItemListFragment;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import butterknife.Bind;

/**
 * @deprecated 暂时没用了 2018/2/7
 * Created by Andrew on 2017/10/21.
 */

public class RecordListActivity extends SwipeBackActivity
        implements RecyclerTClickListener<LedgerBuildingCheckItemVo> {

    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    private static MeCompanyVo companyVo;
    private static LedgerBuildingItemVo ledgerBuildingItemVo;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_COMPANY_VO)) {
                companyVo = (MeCompanyVo) bundle.getSerializable(KeyConstants.KEY_COMPANY_VO);
            }
        }

    }

    @Override
    protected void initTitle() {
        this.setBarTitle(CommonUtil.avoidNullString(companyVo.getCompany_name()));
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(RecordListActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_viewpage_tab_layout;
    }

    @Override
    protected void initViews() {
        super.initViews();

        ledgerBuildingItemVo = new LedgerBuildingItemVo();
        ledgerBuildingItemVo.setBuilding_id(companyVo.getCompany_id());

        // ======= 该变指示栏的长度 =======
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 10, 10); //左右各缩进5dp
            }
        });

        // ======= 该变指示栏的长度 =======
        viewPager.setAdapter(new RecordViewpagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

    }

    private class RecordViewpagerAdapter extends FragmentStatePagerAdapter {

        public RecordViewpagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            LedgerBuildingCheckItemListFragment ledgerBuildingCheckItemListFragment;
            switch (position) {
                case 0:
                    ledgerBuildingCheckItemListFragment = LedgerBuildingCheckItemListFragment.newInstance(ledgerBuildingItemVo);
                    ledgerBuildingCheckItemListFragment.setListener(RecordListActivity.this);
                    return ledgerBuildingCheckItemListFragment; //巡查点位
                case 1:
                    return SafeHiddenListForGovFragment.newInstance(2, 0, companyVo.getCompany_id()); //当前选中的企业全部
                default:
                    ledgerBuildingCheckItemListFragment = LedgerBuildingCheckItemListFragment.newInstance(ledgerBuildingItemVo);
                    ledgerBuildingCheckItemListFragment.setListener(RecordListActivity.this);
                    return ledgerBuildingCheckItemListFragment; //巡查点位
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.record_list_tab_1);
                case 1:
                    return getString(R.string.record_list_tab_2);
            }
            return getString(R.string.record_list_tab_1);
        }

    }

    @Override
    public void onItemClick(LedgerBuildingCheckItemVo data, int position) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_LEDGER_PATROL_CHECK_ITEM_VO, data);
        bundle.putSerializable(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO, ledgerBuildingItemVo);
        UIHelper.showActivity(RecordListActivity.this, bundle, LedgerBuildingCheckItemDetailActivity.class);
    }

}
